package com.example.qcweb;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

import com.example.dao.UserAssignService;
import com.example.dao.UserService;
import com.example.qc.R;
import com.example.zxingweb.CaptureActivity;

import java.text.SimpleDateFormat;

public class signactivity extends TabActivity {

    private Button btcancel, btsign, btn_cancel_scan, btn_scan;
    private EditText etname, etpwd;
    private ImageView qrImgImageView;
    private TabHost tabHost = null;
    private TextView resultTextView;

    private String systemid, activityname, uname, upwd, tablename;
    private UserAssignService uService;
    private UserService userService;
    private String controlname, userrealname;
    private boolean flag = false;// 系统是否存在该用户
    private int usertype;// 用户类型

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // 调用TabActivity的getTabHost()方法获取TabHost对象
        // setContentView(R.layout.activity_sign);
        setwindowsize();
        getwindowTab();
        getparameter();
        findviewControl();
        btcontrolsetonclicker();

    }

    private void btcontrolsetonclicker() {
        // TODO Auto-generated method stub
        btsign.setOnClickListener(clickHandler);
        btcancel.setOnClickListener(clickHandler);
        btn_scan.setOnClickListener(clickHandler);
        btn_cancel_scan.setOnClickListener(clickHandler);
    }

    private void passresult(String signdata) {
        Intent myintent = new Intent();
        myintent.putExtra("signdata", signdata);
        setResult(Activity.RESULT_OK, myintent);
        finish();
    }

    View.OnClickListener clickHandler = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.btsign:
                    uname = etname.getText().toString();
                    upwd = etpwd.getText().toString();
                    flag = new UserService(signactivity.this).login(uname, upwd);
                    usertype = new UserService(signactivity.this).findUserType(
                            uname, upwd);
                    userrealname = new UserService(signactivity.this)
                            .getUserRealName(uname, upwd);
                    uService = new UserAssignService(signactivity.this);
                    if (flag) // 如果用户存在
                    {
                        String signdata = getDate();
                        if (controlname.equals("operator")
                                && (usertype == 1 | usertype == 2)) {
                            uService.OperatorAssign(systemid, signdata,
                                    activityname);
                            passresult(signdata);

                        } else if (controlname.equals("rechecker")
                                && (usertype == 1 | usertype == 2)) {
                            uService.reInnerCheckerAssign(systemid, signdata,
                                    activityname);
                            passresult(signdata);

                        } else if (controlname.equals("checker") && (usertype == 3)) {
                            uService.CheckerAssign(systemid, signdata, activityname);
                            passresult(signdata);
                        } else if (controlname.equals("process") && (usertype == 4)) {
                            uService.TechnicrAssign(systemid, signdata,
                                    activityname);
                        } else if (controlname.equals("designer")) {

                            uService.TechnicrAssign(systemid, signdata,
                                    activityname);
                        } else {
                            new AlertDialog.Builder(signactivity.this)
                                    .setTitle("错误提示").setMessage("您不能在这里签字")
                                    .setPositiveButton("确定", null).show();

                        }
                    } else {
                        new AlertDialog.Builder(signactivity.this).setTitle("错误提示")
                                .setMessage("系统中没有该用户，请联系管理员")
                                .setPositiveButton("确定", null).show();
                    }
                    break;
                case R.id.btn_scan:
                    Intent openCameraIntent = new Intent(signactivity.this,	CaptureActivity.class);
                    startActivityForResult(openCameraIntent, 0);
                    break;

                case R.id.btcancel:
                    finish();
                    break;

                case R.id.btn_cancel_scan:
                    finish();
                    break;
            }
        }
    };
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //处理扫描结果（在界面上显示）
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            resultTextView.setText(scanResult);
        }
    }

    private void setwindowsize() {
        // 自定义窗口大小
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        WindowManager m = getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
        p.height = (int) (d.getHeight() * 0.43); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * 0.4); // 宽度设置为屏幕的0.65
        //设置窗口显示位置
        p.x = -30;
        p.y = -500;
        getWindow().setAttributes(p);

    }

    private void getwindowTab() {
        TabHost tabHost = getTabHost();
        // 设置使用TabHost布局
        LayoutInflater.from(this).inflate(R.layout.activity_sign,
                tabHost.getTabContentView(), true);
        // 添加第一个标签页
        tabHost.addTab(tabHost.newTabSpec("账户登录").setIndicator("账户登录签字")
                .setContent(R.id.layout01));

        // 添加第二个标签页,并在其标签上添加一个图片
        tabHost.addTab(tabHost.newTabSpec("二维码登录").setIndicator("二维码登录签字")
                .setContent(R.id.layout02));
        TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0)
                .findViewById(android.R.id.title);
        tv.setTextSize(16);
        TextView tv1 = (TextView) tabHost.getTabWidget().getChildAt(1)
                .findViewById(android.R.id.title);
        tv1.setTextSize(16);
    }

    private void findviewControl() {
        //Tab1控件
        etname = (EditText) findViewById(R.id.edusername);
        etpwd = (EditText) findViewById(R.id.edpassword);
        btcancel = (Button) findViewById(R.id.btcancel);
        btsign = (Button) findViewById(R.id.btsign);

        //Tab2控件
        qrImgImageView = (ImageView) findViewById(R.id.igewm);
        btn_cancel_scan = (Button) findViewById(R.id.btn_cancel_scan);
        btn_scan = (Button) findViewById(R.id.btn_scan);
        resultTextView= (TextView) findViewById(R.id.resultTextView);


    }

    private void getparameter() {
        // TODO Auto-generated method stub

        Bundle mybuddle = this.getIntent().getExtras();
        systemid = mybuddle.getString("systemid");
        activityname = mybuddle.getString("activityname");
        controlname = mybuddle.getString("controlname");
        Log.i("参数信息", systemid + activityname + controlname);
    }

    private String getDate() {

        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = sDateFormat.format(new java.util.Date());
        return userrealname + " " + date;

    }


}
